package com.example.yam.greenapp;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;

import java.util.List;

/**
 * Created by 杨建利 on 2017/2/2
 */

public class DingdansAdapter extends ArrayAdapter<Dingdan> {
    private int resourceId;
    public DingdansAdapter(Context context, int textViewResourceId, List objects) {
        super(context,textViewResourceId, objects);
        this.resourceId=textViewResourceId;
    }
    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       Dingdan dingdan=getItem(position);
        View view;
        ViewHoder viewHoder;
        if(convertView == null) {//如果没有进行过布局

            view = LayoutInflater.from(getContext()).inflate(resourceId, null);

            viewHoder = new ViewHoder();

            viewHoder.dingdan_time = (TextView) view.findViewById(R.id.dingdan_time);
            viewHoder.dingdan_dnum = (TextView) view.findViewById(R.id.dingdan_dnum);
            viewHoder.dingdan_receivermessage = (TextView) view.findViewById(R.id.dingdan_receivermessage);
            viewHoder.dingdan_total = (TextView) view.findViewById(R.id.dingdan_total);
            viewHoder.dingdan_flag = (TextView) view.findViewById(R.id.dingdan_flag);
            //将viewHoder存储在view的Tag中
            view.setTag(viewHoder);
        }else {
            view = convertView;
            viewHoder = (ViewHoder) view.getTag();//重新获取viewhoder
        }
        viewHoder.dingdan_time.setText("下单时间:"+dingdan.getTime());
        viewHoder.dingdan_dnum.setText("订单号:"+dingdan.getDnum());
        viewHoder.dingdan_receivermessage.setText("收货信息:\n"+dingdan.getAddress()+"("+dingdan.getReceiver()+")"+dingdan.getPhone());
        viewHoder.dingdan_total.setText("总价:"+dingdan.getTotal()+"元");

        String paied = "未付款";
        if(dingdan.getFlag().equals("1")) paied = "已付款";
        viewHoder.dingdan_flag.setText(paied);
        return view;
    }
    class ViewHoder{//缓存控件
        TextView dingdan_time;//订单号
        TextView dingdan_dnum;//订单号
        TextView dingdan_receivermessage;//收件人信息
        TextView dingdan_total;//总价
        TextView dingdan_flag;//付款信息
    }

}
